package com.oa.ylms.apps.business.account.impl;

import java.util.List;

import com.oa.ylms.apps.business.account.IAccountCheckService;
import com.oa.ylms.apps.business.account.IAccountInfoService;
import com.oa.ylms.apps.business.plan.ICheckPlanService;
import com.oa.ylms.apps.common.DateUtil;
import com.oa.ylms.apps.constants.ICheckStates;
import com.oa.ylms.apps.constants.IPlanCategory;
import com.oa.ylms.apps.constants.account.IAccountInfoStates;
import com.oa.ylms.apps.constants.IOrderStates;
import com.oa.ylms.apps.dao.account.IAccountInfoDao;
import com.oa.ylms.apps.entity.account.AccountCheckValue;
import com.oa.ylms.apps.entity.account.AccountInfoValue;
import com.oa.ylms.apps.entity.archives.StaffInfoValue;
import com.oa.ylms.apps.entity.plan.CheckPlanValue;

public class AccountInfoServiceImpl implements IAccountInfoService{
	private IAccountInfoDao accountDao;
	private AccountCheckValue accountCheck;
	private AccountInfoValue accountInfo;
	private ICheckPlanService checkPlanService;
	private IAccountCheckService accountCheckService;
	
	private Double inTotal = 0.0;
	private Double outTotal = 0.0;

	@Override
	public List<AccountInfoValue> queryAccountInfoWithPage(StaffInfoValue staffInfo,Integer inOrOut,Integer states,
			int rows, int pageNo) throws Exception {
		if(staffInfo!=null){
			accountInfo.setStaffInfo(staffInfo);
			}
		if(inOrOut!=0){
		accountInfo.setInOrOut(inOrOut);
		}
		if(states!=0){
			accountInfo.setStates(states);
		}
		return accountDao.queryAccountInfoWithPage(accountInfo, rows, pageNo);
	}
	@Override
	public int countTPages(StaffInfoValue staffInfo, Integer inOrOut,Integer states, int rows)
			throws Exception {
		if(staffInfo!=null){
		accountInfo.setStaffInfo(staffInfo);
		}
		if(inOrOut!=0){
		accountInfo.setInOrOut(inOrOut);
		}
		if(states!=0){
			accountInfo.setStates(states);
		}
		return accountDao.countTpages(accountInfo, rows);
	}


	@Override
	public int addAccountInfo(StaffInfoValue staffInfo,
			AccountInfoValue accountInfo) throws Exception {
		int postId=staffInfo.getArchiveInfo().getPostInfo().getPostId();
		CheckPlanValue checkPlan = (CheckPlanValue) checkPlanService.queryByPostIdAndCategoryId(IPlanCategory.ACCOUNTCHECK, postId);
	if(checkPlan!=null){
		String str = checkPlan.getPlanOrders();
		String[] deptNo = str.split(",");
		accountInfo.setCheckPlan(checkPlan);
		accountInfo.setStates(Integer.parseInt(deptNo[0]));
	}
		accountInfo.setCreateTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		accountInfo.setStaffInfo(staffInfo);
		return accountDao.addAccountInfo(accountInfo);
	}

	@Override
	public List<AccountInfoValue> queryAccountCheck(StaffInfoValue staffInfo,int inOrOut)
			throws Exception {
		int sectionId = staffInfo.getArchiveInfo().getPostInfo()
				.getSectionInfo().getSectionId();
		accountInfo.setStates(sectionId);// 查询状态等于部门号
		if(inOrOut!=0){
		accountInfo.setInOrOut(inOrOut);
		}
		return accountDao.queryAccountCheck(accountInfo);
	}

	@Override
	public AccountInfoValue queryById(Integer id) throws Exception {
		accountInfo.setAccountId(id);
		return accountDao.queryById(accountInfo);
	}

	@Override
	public void checkPass(StaffInfoValue staffInfo,Integer id, String comments) throws Exception {
		AccountInfoValue account = queryById(id);
		String str = account.getCheckPlan().getPlanOrders();
		String[] deptNo = str.split(",");
		int states = account.getStates();
		for (int i = 0; i < deptNo.length; i++) {
			int index = Integer.parseInt(deptNo[i]);
			if (states == index && i != deptNo.length - 1) {
				account.setStates(Integer.parseInt(deptNo[i + 1]));
				accountDao.modifyAccountInfo(account);
				accountCheck.setAccountInfo(account);
				accountCheck.setCheckTime(DateUtil.toDate(DateUtil
						.getCurrentDate()));
				accountCheck.setStaffInfo(staffInfo);
				accountCheck.setComments(comments);
				accountCheck.setCheckOrder(states);
				accountCheck.setIsPassed(ICheckStates.CHECKPASSED);
				accountCheckService.addAccountCheck(accountCheck);
				break;
			}
			if (i == deptNo.length - 1) {
				account.setStates(IOrderStates.PASSED);
				accountDao.modifyAccountInfo(account);
				accountCheck.setAccountInfo(account);
				accountCheck.setStaffInfo(staffInfo);
				accountCheck.setComments(comments);
				accountCheck.setCheckTime(DateUtil.toDate(DateUtil
						.getCurrentDate()));
				accountCheck.setCheckOrder(states);
				accountCheck.setIsPassed(ICheckStates.CHECKPASSED);
				accountCheckService.addAccountCheck(accountCheck);
			}
		}
	}

	@Override
	public void checkUnpass(StaffInfoValue staffInfo,Integer id, String comments) throws Exception {
		accountInfo.setAccountId(id);
		AccountInfoValue account = accountDao.queryById(accountInfo);
		accountCheck.setAccountInfo(account);
		accountCheck.setStaffInfo(staffInfo);
		accountCheck.setComments(comments);
		accountCheck.setCheckTime(DateUtil.toDate(DateUtil.getCurrentDate()));
		accountCheck.setCheckOrder(account.getStates());
		accountCheck.setIsPassed(ICheckStates.CHECKFAILED);
		accountCheckService.addAccountCheck(accountCheck);
		account.setStates(IOrderStates.FAILED);
		accountDao.modifyAccountInfo(account);
	}

	public List<AccountInfoValue> queryAccountInfo(String inOrOut,
			String startDate, String endDate, String title, String posters,
			String state) throws Exception {

		return accountDao.queryAccountInfo(inOrOut, startDate, endDate, title,
				posters, state);
	}

	public List<AccountInfoValue> queryAccountInfo1(Integer inOrOut,
			String startDate, String endDate, String title, String posterName,
			Integer state) throws Exception {
		String staffIds = "";
		if (inOrOut != null) {
			accountInfo.setInOrOut(inOrOut);
		}
		if (title != null) {
			accountInfo.setTitle(title);
		}
		if (posterName != null) {
			List<StaffInfoValue> list =null;
			if (list != null) {
				for (StaffInfoValue staffInfo : list) {
					int staffId = staffInfo.getStaffId();
					staffIds = staffId + ",";
				}
				staffIds = staffIds.substring(0, staffIds.length() - 1);
			}
		}
		if (state != null) {
			accountInfo.setStates(state);
		}
		return accountDao.queryAccountInfo1(accountInfo, startDate, endDate,
				staffIds);
	}
	@Override
	public int getTotalNumber() {
		return accountDao.getTotalNumber();
	}
	
	@Override
	public boolean queryAccountInfo(Integer year, Integer month,
			Integer inOrOut) throws Exception {
		inTotal = 0.0;
		outTotal = 0.0;
		countTotal(accountDao.queryAccountInfo(year, month, inOrOut));
		if(inTotal != null && outTotal != null){
			return true;
		}
		return false;
	}
	
	@Override
	public List<AccountInfoValue> queryAccountList(Integer year, Integer month,
			Integer inOrOut) throws Exception {
		return accountDao.queryAccountInfo(year, month, inOrOut);
	}
	
	@Override
	public void countTotal(List<AccountInfoValue> list) throws Exception{
		for(AccountInfoValue accountInfo : list){
			if(accountInfo.getInOrOut() == IAccountInfoStates.ACCOUNTIN){
				inTotal += accountInfo.getPrice();
			}
			if(accountInfo.getInOrOut() == IAccountInfoStates.ACCOUNTOUT){
				outTotal += accountInfo.getPrice();
			}
		}
	}
	
	@Override
	public double getInTotalInfo() {
		return getInTotal();
	}
	@Override
	public double getOutTotalInfo() {
		return getOutTotal();
	}


	public IAccountInfoDao getAccountDao() {
		return accountDao;
	}

	public void setAccountDao(IAccountInfoDao accountDao) {
		this.accountDao = accountDao;
	}

	public AccountCheckValue getAccountCheck() {
		return accountCheck;
	}

	public void setAccountCheck(AccountCheckValue accountCheck) {
		this.accountCheck = accountCheck;
	}

	public AccountInfoValue getAccountInfo() {
		return accountInfo;
	}

	public void setAccountInfo(AccountInfoValue accountInfo) {
		this.accountInfo = accountInfo;
	}

	public ICheckPlanService getCheckPlanService() {
		return checkPlanService;
	}

	public void setCheckPlanService(ICheckPlanService checkPlanService) {
		this.checkPlanService = checkPlanService;
	}

	public IAccountCheckService getAccountCheckService() {
		return accountCheckService;
	}

	public void setAccountCheckService(IAccountCheckService accountCheckService) {
		this.accountCheckService = accountCheckService;
	}
	
	public void setInTotal(Double inTotal) {
		this.inTotal = inTotal;
	}
	public Double getInTotal() {
		return inTotal;
	}
	public Double getOutTotal() {
		return outTotal;
	}
	public void setOutTotal(Double outTotal) {
		this.outTotal = outTotal;
	}
}
